package me.liuhui.mall.common.image.impl;

import cn.hutool.core.img.ImgUtil;
import me.liuhui.mall.common.image.BatchConverter;
import me.liuhui.mall.common.image.ImageInfo;
import me.liuhui.mall.common.image.ImageProcessor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class LocalImageProcessorImpl implements ImageProcessor {
    @Override
    public BatchConverter batch() {
        throw new RuntimeException("不支持此操作");
    }

    @Override
    public ImageInfo identify(String imagePath) {
        return null;
    }

    @Override
    public boolean waterMarker(String srcFilename, String destFilename, String waterFilePath, int opacity, String position) {
        try {
            ImgUtil.pressImage(new File(srcFilename), new File(destFilename), ImageIO.read(new File(waterFilePath)), 0, 0, opacity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public boolean resize(String inFilePath, String outFilePath, Integer width, Integer height, boolean fillWhite) {
        ImgUtil.scale(new File(inFilePath), new File(outFilePath), width, height, Color.WHITE);
        return true;
    }

    @Override
    public boolean resizeByScale(String inFilePath, String outFilePath, Integer width, Integer height) {
        try {
            Image outImg = ImgUtil.scale(ImageIO.read(new File(inFilePath)), width, height);
            ImgUtil.write(outImg, new File(outFilePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public boolean crop(String inFilePath, String outFilePath, int x, int y, int width, int height) {
        ImgUtil.cut(new File(inFilePath), new File(outFilePath), new Rectangle(x, y, width, height));
        return true;
    }

    @Override
    public boolean compress(String srcFilename, Double quality, String destFilename) {
        ImgUtil.compress(new File(srcFilename), new File(destFilename), quality.floatValue() / 100);
        return true;
    }

    @Override
    public boolean rotate(String srcFilename, double angle, String destFilename) {
        ImgUtil.rotate(new File(srcFilename), (int) angle, new File(destFilename));
        return true;
    }

    @Override
    public boolean blur(String srcFilename, double scale, String destFilename) {
        throw new RuntimeException("不支持此操作");

    }

    @Override
    public boolean convert(String srcFilename, String destFilename) {
        try {
            ImgUtil.write(ImageIO.read(new File(srcFilename)), new File(destFilename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public boolean flip(String srcFilename, String destFilename) {
        throw new RuntimeException("不支持此操作");
    }

    @Override
    public boolean flop(String srcFilename, String destFilename) {
        throw new RuntimeException("不支持此操作");
    }
}
